package com.cloud.phone.tools;

import android.accessibilityservice.AccessibilityService;
import android.graphics.Rect;
import android.view.accessibility.AccessibilityNodeInfo;

import java.util.List;

public class AutoUtil {
   private static AutoUtil autoUtil;
    public AccessibilityService service;

    public static AutoUtil getInstance() {
        if(autoUtil ==null){
            autoUtil =new AutoUtil();
        }
        return autoUtil;
    }

    public void setService(AccessibilityService service) {
        this.service = service;
    }


    /**
     * 查找按钮，返回是否成功
     * @return
     */
    public boolean isNodeExist(String idStr,int index){
        List<AccessibilityNodeInfo> nodes = AccessibilityTool.getInstance().findNodesById(service, idStr);
        if(AccessibilityTool.getInstance().isNodeExist(nodes)){
            AccessibilityNodeInfo currentNode=nodes.get(0);
            if(nodes.size()>index){
                currentNode=nodes.get(index);
            }
            if(currentNode!=null){
                return true;
            }else{
                return false;
            }
        }else{
            return false;
        }
    }

    /**
     * 查找按钮，返回是否成功
     * @return
     */
    public boolean isNodeExist(String idStr){
        List<AccessibilityNodeInfo> nodes = AccessibilityTool.getInstance().findNodesById(service, idStr);
        if(AccessibilityTool.getInstance().isNodeExist(nodes)){
            AccessibilityNodeInfo currentNode=nodes.get(0);
            if(currentNode!=null){
                return true;
            }else{
                return false;
            }
        }else{
            return false;
        }
    }


    /**
     * 查找按钮，返回是否成功
     * @return
     */
    public boolean isNodeExistByText(String textStr,int index){
        List<AccessibilityNodeInfo> nodes = AccessibilityTool.getInstance().findNodesByText(null,null,service, textStr);
        if(AccessibilityTool.getInstance().isNodeExist(nodes)){
            AccessibilityNodeInfo currentNode=nodes.get(0);
            if(nodes.size()>index){
                currentNode=nodes.get(index);
            }
            if(currentNode!=null){
                return true;
            }else{
                return false;
            }
        }else{
            return false;
        }
    }

    /**
     * 查找按钮，返回是否成功
     * @return
     */
    public boolean isNodeExistByText(String textStr){
        List<AccessibilityNodeInfo> nodes = AccessibilityTool.getInstance().findNodesByText(null,null,service, textStr);
        if(AccessibilityTool.getInstance().isNodeExist(nodes)){
            AccessibilityNodeInfo currentNode=nodes.get(0);
            if(currentNode!=null){
                return true;
            }else{
                return false;
            }
        }else{
            return false;
        }
    }


    /**
     * 查找并点击按钮，返回是否点击成功
     * @return
     */
    public boolean findNodeByIdAndClick(String idStr,int index){
        List<AccessibilityNodeInfo> nodes = AccessibilityTool.getInstance().findNodesById(service, idStr);
        if(AccessibilityTool.getInstance().isNodeExist(nodes)){
            AccessibilityNodeInfo currentNode=nodes.get(0);
            if(nodes.size()>index){
                currentNode=nodes.get(index);
            }
            if(currentNode!=null){
                Rect rect = new Rect();
                currentNode.getBoundsInScreen(rect);
                float left = rect.left;
                float right = rect.right;
                float top = rect.top;
                float bottom = rect.bottom;
                int x = (int) ((left + right) / 2);
                int y = (int) ((top + bottom) / 2);
                if(y<0){
                    y= (int) (Math.abs(top-bottom)*2);
                }
                AccessibilityTool.getInstance().clickScreen(service,x,y);
                return true;
            }else{
                return false;
            }
        }else{
            return false;
        }
    }

    /**
     * 查找并点击按钮，返回是否点击成功
     * @param nodeId
     * @return
     */
    public boolean findNodeByIdAndClick(String nodeId){
        AccessibilityNodeInfo currentNode = AccessibilityTool.getInstance().findNodeFirstById(service, nodeId);
        if(currentNode!=null){
            Rect rect = new Rect();
            currentNode.getBoundsInScreen(rect);
            float left = rect.left;
            float right = rect.right;
            float top = rect.top;
            float bottom = rect.bottom;
            int x = (int) ((left + right) / 2);
            int y = (int) ((top + bottom) / 2);
            if(y<0){
                y= (int) (Math.abs(top-bottom)*2);
            }
            AccessibilityTool.getInstance().clickScreen(service,x,y);
            return true;
        }else{
            return false;
        }
    }

    /**
     * 查找并点击按钮，返回是否点击成功
     * @param text
     * @return
     */
    public boolean findNodeByTextAndClick(String text){
        List<AccessibilityNodeInfo> nodes = AccessibilityTool.getInstance().findNodesByText(null, null, service, text);
        if(AccessibilityTool.getInstance().isNodeExist(nodes)){
            AccessibilityNodeInfo currentNode=nodes.get(0);
            if(currentNode!=null){
                Rect rect = new Rect();
                currentNode.getBoundsInScreen(rect);
                float left = rect.left;
                float right = rect.right;
                float top = rect.top;
                float bottom = rect.bottom;
                int x = (int) ((left + right) / 2);
                int y = (int) ((top + bottom) / 2);
                if(y<0){
                    y= (int) (Math.abs(top-bottom)*2);
                }
                AccessibilityTool.getInstance().clickScreen(service,x,y);
                return true;
            }else{
                return false;
            }
        }else{
            return false;
        }
    }


    /**
     * 查找并点击按钮，返回是否点击成功
     * @param text
     * @return
     */
    public boolean findNodeByTextAndClick(String text,int index){
        List<AccessibilityNodeInfo> nodes = AccessibilityTool.getInstance().findNodesByText(null, null, service, text);
        if(AccessibilityTool.getInstance().isNodeExist(nodes)){
            AccessibilityNodeInfo currentNode=nodes.get(0);
            if(nodes.size()>index){
              currentNode=nodes.get(index);
            }
            if(currentNode!=null){
                Rect rect = new Rect();
                currentNode.getBoundsInScreen(rect);
                float left = rect.left;
                float right = rect.right;
                float top = rect.top;
                float bottom = rect.bottom;
                int x = (int) ((left + right) / 2);
                int y = (int) ((top + bottom) / 2);
                if(y<0){
                    y= (int) (Math.abs(top-bottom)*2);
                }
                AccessibilityTool.getInstance().clickScreen(service,x,y);
                return true;
            }else{
                return false;
            }
        }else{
            return false;
        }
    }


    /**
     * 查找并点击按钮，返回是否点击成功
     * @param nodeId
     * @return
     */
    public boolean findNodeByIdAndSetNodeText(String nodeId,String text){
        AccessibilityNodeInfo currentNode = AccessibilityTool.getInstance().findNodeFirstById(service, nodeId);
        if(currentNode!=null){
            AccessibilityTool.getInstance().setNodeText(currentNode,text);
            return true;
        }else{
            return false;
        }
    }


    /**
     * 查找并点击按钮，返回是否点击成功
     * @param nodeId
     * @return
     */
    public boolean findNodeByIdAndSetNodeText(String nodeId,String text,int index){
        List<AccessibilityNodeInfo> nodes = AccessibilityTool.getInstance().findNodesById(service, nodeId);
        if(AccessibilityTool.getInstance().isNodeExist(nodes)){
            AccessibilityNodeInfo currentNode=nodes.get(0);
            if(index<nodes.size()){
                currentNode=nodes.get(index);
            }
            if(currentNode!=null){
                AccessibilityTool.getInstance().setNodeText(currentNode,text);
                return true;
            }else{
                return false;
            }
        }else{
            return false;
        }
    }


    /**
     * 查找并点击按钮，返回是否点击成功
     * @return
     */
    public boolean findNodeByTextAndSetNodeText(String nodeKeyText,String text){
        List<AccessibilityNodeInfo> nodes  = AccessibilityTool.getInstance().findNodesByText(null,null,service, nodeKeyText);
        if(AccessibilityTool.getInstance().isNodeExist(nodes)) {
            AccessibilityNodeInfo currentNode = nodes.get(0);
            if(currentNode!=null){
                AccessibilityTool.getInstance().setNodeText(currentNode,text);
                return true;
            }else{
                return false;
            }
        }else{
            return false;
        }
    }


    /**
     * 查找并设置文本，返回是否设置文本成功
     * @return
     */
    public boolean findNodeByTextAndSetNodeText(String nodeKeyText,String text,int index){
        List<AccessibilityNodeInfo> nodes  = AccessibilityTool.getInstance().findNodesByText(null,null,service, nodeKeyText);
        if(AccessibilityTool.getInstance().isNodeExist(nodes)){
            AccessibilityNodeInfo currentNode=nodes.get(0);
            if(index<nodes.size()){
                currentNode=nodes.get(index);
            }
            if(currentNode!=null){
                AccessibilityTool.getInstance().setNodeText(currentNode,text);
                return true;
            }else{
                return false;
            }
        }else{
            return false;
        }
    }

}
